package com.bamatea.reportfrom.controller;

import com.bamatea.reportfrom.domain.*;
import com.bamatea.reportfrom.service.BdInvclWccService;
import com.bamatea.reportfrom.service.BdinvclWcSercive;
import com.bamatea.reportfrom.service.IcgeneralAllWcService;
import com.bamatea.reportfrom.service.ReportService;
import com.bamatea.reportfrom.util.ResponseUtil;
import com.bamatea.reportfrom.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

/**
 * @Auther: libingbing
 * @Date: 2020-04-24 14 :50
 * @Description:
 */
@RestController
@RequestMapping("/bama")
public class ReportFormController {
    @Autowired
    private BdinvclWcSercive sercive;
    @Autowired
    private IcgeneralAllWcService icgeneralAllWcService;
    @Autowired
    private com.bamatea.reportfrom.service.poOrderBwcService poOrderBwcService;
    @Autowired
    private BdInvclWccService bdInvclWccService;
    @Autowired
    private com.bamatea.reportfrom.service.icRecordsignWcService icRecordsignWcService;
    @Autowired
    private ExecutorService executorService;
    @Autowired
    private ReportService reportService;

    //获取所有分类
    @GetMapping("/getCategory")
    public Object getCategory() {
        List<BdInvclWc> allCategory = sercive.findAllCategory();
        List<String> data = new ArrayList<>();
        for (BdInvclWc item : allCategory) {
            data.add(item.getInvclassname());
        }
        return ResponseUtil.okList(data);
    }

    //获取所有分类2
    @GetMapping("/getCategory2")
    public Object getFromCategory() {
        List<BdInvclWcC> allCategory = bdInvclWccService.findAllCategory();
        List<String> data = new ArrayList<>();
        for (BdInvclWcC item : allCategory) {
            data.add(item.getInvclassname());
        }
        return ResponseUtil.okList(data);
    }

    //获取库存金额占比
    @PostMapping("/getCategoryAmount")
    public Object getCategoryAmount(@RequestParam("start") String start, @RequestParam("end") String end) {
        BigDecimal sumAmount = new BigDecimal(0.00);
        List<IcGeneralAllWc> list = icgeneralAllWcService.findByStartAndEnd(start, end);
        for (IcGeneralAllWc item : list) {
            //入库数量
            BigDecimal ninnum = item.getNinnum();
            if (ninnum == null) {
                ninnum = new BigDecimal(0.00);
            }
            //出库数量
            BigDecimal noutnum = item.getNoutnum();
            if (noutnum == null) {
                noutnum = new BigDecimal(0.00);
            }
            //库存数
            BigDecimal catNum = ninnum.subtract(noutnum);
            PoOrderBWc bySku = poOrderBwcService.findBySku(item.getInvcode());
            BigDecimal norgtaxprice = null;
            if (bySku != null) {
                //商品单价
                norgtaxprice = bySku.getNorgtaxprice();
            } else {
                norgtaxprice = new BigDecimal(0.00);
            }
            //一个商品的进货价
            BigDecimal multiply = norgtaxprice.multiply(catNum);
            sumAmount = sumAmount.add(multiply);
        }

        BigDecimal cateAmount = new BigDecimal(0.00);
        //获取所有分类的vo
        List<CategoryVo> categoryVos = sercive.findAllCatClassCode();
        for (CategoryVo item : categoryVos) {
            String code = item.getInvClassCode();
            List<IcGeneralAllWc> icGeneralAllWcs = icgeneralAllWcService.findByCatId(code);
            for (IcGeneralAllWc item2 : icGeneralAllWcs) {
                //入库数量
                BigDecimal ninnum2 = item2.getNinnum();
                //出库数量
                BigDecimal noutnum2 = item2.getNoutnum();
                if (ninnum2 == null) {
                    ninnum2 = new BigDecimal(0.00);
                }
                if (noutnum2 == null) {
                    noutnum2 = new BigDecimal(0.00);
                }
                //库存数
                BigDecimal catNum2 = null;
                if (ninnum2 != null && noutnum2 != null) {
                    catNum2 = ninnum2.subtract(noutnum2);
                }
                PoOrderBWc bySku2 = poOrderBwcService.findBySku(item2.getInvcode());
                if (bySku2 != null) {
                    //商品单价
                    BigDecimal norgtaxprice2 = bySku2.getNorgtaxprice();
                    if (norgtaxprice2 == null) {
                        norgtaxprice2 = new BigDecimal(0.00);
                    }
                    BigDecimal multiply2 = null;
                    if (norgtaxprice2 != null) {
                        //一个商品的进货价
                        multiply2 = norgtaxprice2.multiply(catNum2);
                    }
                    cateAmount = cateAmount.add(multiply2);
                }
            }
            item.setInvClassAmount(cateAmount);
            if (sumAmount.intValue() > 0) {
                item.setRatio(cateAmount.divide(sumAmount));
            }
        }
        HashMap<String, Object> result = new HashMap<>();
        result.put("sumAmount", sumAmount);
        result.put("categoryVos", categoryVos);
        return ResponseUtil.ok(result);
    }

    @PostMapping("/getCategoryAmount2")
    public Object getCategoryAmount2(@RequestParam("start") String start, @RequestParam("end") String end) {
        List<IcGeneralAllWc> list = icgeneralAllWcService.findByStartAndEnd(start, end);
        Map<String, Object> map = new HashMap<>();
        BigDecimal amount = new BigDecimal(0.00);
        BigDecimal amount2 = new BigDecimal(0.00);
        for (IcGeneralAllWc item : list) {
            //商品编码
            String invcode = item.getInvcode();
            //2019年之前的库存数据
            IcRecordsignWc byinvcode = icRecordsignWcService.findByinvcode(invcode);
            Double ninnum1 = 0.00;
            Double noutnum1 = 0.00;
            if (byinvcode != null) {
                ninnum1 = byinvcode.getNinnum();
                noutnum1 = byinvcode.getNoutnum();
                if (ninnum1 == null) {
                    ninnum1 = 0.00;
                }
                if (noutnum1 == null) {
                    noutnum1 = 0.00;
                }
            } else {
                ninnum1 = 0.00;
                noutnum1 = 0.00;
            }
            //2019年之后的库存入库数量
            BigDecimal ninnum = item.getNinnum();
            if (ninnum == null) {
                ninnum = new BigDecimal(0.00);
            }
            ////2019年之后的库存出库数量
            BigDecimal noutnum = item.getNoutnum();
            if (noutnum == null) {
                noutnum = new BigDecimal(0.00);
            }
            PoOrderBWc bySku = poOrderBwcService.findBySku(invcode);
            if (bySku != null) {
                //商品单价
                BigDecimal norgtaxprice = bySku.getNorgtaxprice();
                BigDecimal nums = (ninnum.add(new BigDecimal(ninnum1)).subtract(noutnum.add(new BigDecimal(noutnum1))));

                BigDecimal res = norgtaxprice.multiply(nums);
                amount = amount.add(res);
            } else {
                BigDecimal norgtaxprice = new BigDecimal(0.00);
                BigDecimal nums = ninnum.subtract(noutnum);

                BigDecimal res = norgtaxprice.multiply(nums);
                amount = amount.add(res);
            }
        }
        //----------------------------------------------
        //查询每个分类
        List<BdInvclWc> category = sercive.findAllCategory();
        List<String> list2 = new ArrayList();
        for (BdInvclWc item : category) {
            list2.add(item.getOrdinarytype());
        }
        for (int i = 0; i < list.size(); i++) {
            List<BdInvclWc> all = sercive.findByName(list2.get(i));
            List<String> list3 = new ArrayList<>();
            for (BdInvclWc item : all) {
                list3.add(item.getInvclasscode());
            }
            List<IcGeneralAllWc> bycodes = icgeneralAllWcService.findBycodes(list2);
            for (IcGeneralAllWc item : bycodes) {
                //商品编码
                String invcode1 = item.getInvcode();
                //2019年之前的库存数据
                IcRecordsignWc byinvcode1 = icRecordsignWcService.findByinvcode(invcode1);
                Double ninnum3 = 0.00;
                Double noutnum3 = 0.00;
                if (byinvcode1 != null) {
                    ninnum3 = byinvcode1.getNinnum();
                    noutnum3 = byinvcode1.getNoutnum();
                    if (ninnum3 == null) {
                        ninnum3 = 0.00;
                    }
                    if (noutnum3 == null) {
                        noutnum3 = 0.00;
                    }
                } else {
                    ninnum3 = 0.00;
                    noutnum3 = 0.00;
                }
                BigDecimal ninnum4 = item.getNinnum();
                if (ninnum4 == null) {
                    ninnum4 = new BigDecimal(0.00);
                }
                ////2019年之后的库存出库数量
                BigDecimal noutnum4 = item.getNoutnum();
                if (noutnum4 == null) {
                    noutnum4 = new BigDecimal(0.00);
                }
                PoOrderBWc bySku2 = poOrderBwcService.findBySku(invcode1);
                if (bySku2 != null) {
                    //商品单价
                    BigDecimal norgtaxprice = bySku2.getNorgtaxprice();
                    BigDecimal nums = (ninnum4.add(new BigDecimal(ninnum3)).subtract(noutnum4.add(new BigDecimal(noutnum3))));

                    BigDecimal res = norgtaxprice.multiply(nums);
                    amount2 = amount2.add(res);
                } else {
                    BigDecimal norgtaxprice = new BigDecimal(0.00);
                    BigDecimal nums = ninnum4.subtract(noutnum4);

                    BigDecimal res = norgtaxprice.multiply(nums);
                    amount2 = amount2.add(res);
                }
                map.put(list2.get(i), amount2.divide(amount));
            }

        }
        Map<String, Object> result = new HashMap<>();
        map.put("amount", amount);
        map.put("map", map);
        return ResponseUtil.ok(result);
    }

    @PostMapping("/getCategoryAmount3")
    public Object getCategoryAmount3(@RequestParam("start") String start, @RequestParam("end") String end) {
        return ResponseUtil.ok(reportService.getCategoryAmount2(start, end));
    }
}